package fussbudget;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.joda.money.Money;
import org.joda.time.DateMidnight;

import fussbudget.SQL.FBSQL;

public class SimpleTransaction implements Comparable {
	public enum MODE {
		DATE,
		AMOUNT
	}

	public static MODE		searchMode	= MODE.DATE;

	private int				id			= -1;
	private DateMidnight	date		= new DateMidnight();
	private Money			amount		= Money.parse("USD 0.00");

	public SimpleTransaction() {
		super();
		id = FBSQL.addSimpleTransaction(this);
	}

	public SimpleTransaction(ResultSet rs) {
		try {
			while (rs.next()) {
				id = rs.getInt("id");
				date = new DateMidnight(rs.getLong("date"));
				amount = Money.parse("USD " + rs.getBigDecimal("amount"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public int compareTo(Object b) {
		SimpleTransaction other = (SimpleTransaction) b;
		switch (searchMode) {
			case DATE:
				return date.compareTo(other.getDate());
			case AMOUNT:
				return amount.compareTo(other.getAmount());
		}
		return 0;
	}

	/**
	 * @return the searchMode
	 */
	public static synchronized MODE getSearchMode() {
		return searchMode;
	}

	/**
	 * @param searchMode
	 *            the searchMode to set
	 */
	public static synchronized void setSearchMode(MODE searchMode) {
		SimpleTransaction.searchMode = searchMode;
	}

	public synchronized int getId() {
		return id;
	}

	public synchronized void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the amount
	 */
	public synchronized Money getAmount() {
		return amount;
	}

	/**
	 * @param amount
	 *            the amount to set
	 */
	public synchronized void setAmount(Money amount) {
		this.amount = amount;
	}

	/**
	 * @return the date
	 */
	public synchronized DateMidnight getDate() {
		return date;
	}

	/**
	 * @param date
	 *            the date to set
	 */
	public synchronized void setDate(DateMidnight date) {
		this.date = date;
	}
}
